package com.info.protocol.serial;


import com.alibaba.fastjson.JSON;
import com.info.protocol.enums.SerializeTypeEnum;

import java.nio.charset.StandardCharsets;


public class JsonSerializer implements Serializer {
    @Override
    public <T> byte[] serialize(T obj) {
        return JSON.toJSONString(obj).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clz) {
        final String content = new String(data);
        return JSON.parseObject(content, clz);
    }

    @Override
    public byte getType() {
        return SerializeTypeEnum.JSON.getCode();
    }
}
